package com.foruo.demo.spring.redis;


import com.foruo.demo.spring.redis.config.RedisConfig;
import com.foruo.demo.spring.redis.entity.DemoEntity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RedisConfig.class)
public class TestMe {

    @Autowired
    RedisConnectionFactory redisConnectionFactory;
    @Autowired
    RedisTemplate<String, DemoEntity> redisTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Test
    public void testMe(){
        /** 使用 RedisConnection */
        RedisConnection connection = redisConnectionFactory.getConnection();
        connection.set("name".getBytes(), "gaoyuan".getBytes());

        System.out.println(new String(connection.get("name".getBytes())));
        connection.close();

        /** 使用 RedisTemplate */
        DemoEntity demoEntity = new DemoEntity("1", "高元", "男");
        redisTemplate.opsForList().rightPush("lkey", demoEntity);
//        demoEntity = redisTemplate.opsForList().rightPop("lkey");
        System.out.println(demoEntity.getName());

        /** 使用 StringRedisTemplate */
        stringRedisTemplate.opsForValue().set("name" ,"高元2");
        System.out.println(stringRedisTemplate.opsForValue().get("name"));
    }

}